<?php
class search{
	function getFileList($keys,$style= 0){
		global $dbs , $user , $page , $fromType,$encrypt ;
		$style = (int)$style;
		$keyWords = array();
		if( 1 == $style  ){// 按标签
		
		
		}elseif( 2 == $style ){//按上传者
			$userArray =array();
			$sql = 'SELECT * FROM `'.DB_PRE.'user` WHERE `userName` like "%'. $keys .'%" OR `nickName` like "%'. $keys .'%" ';
			$lists = $dbs -> select($sql);
/*			echo '<pre>';
			print_r($lists);
			echo '</pre>';*/
			$i = 0 ;
			$isMyIn = false;
			foreach($lists as $list){
				if( $i >= 5 )
					break ; // 限定相似用户
				if($list['id'] == $user -> id() )
					$isMyIn = true;
				$sqlCount .= 'OR (`user_id` = '. $list['id'] .' AND `status` = "1") '  ;
				$i++ ;
			}

			$sqlCount = substr($sqlCount ,3);

			if( '' != $sqlCount ){
				
				if( $user -> powder() >= 9  )
					$sqlCount = ' WHERE '.$sqlCount;
				else{
					if($isMyIn)
						$sqlCount = ' WHERE ('.$sqlCount .') OR ( `user_id` = '.$user -> id() .' ) ORDER BY `id` DESC';
					else
						$sqlCount = ' WHERE ('.$sqlCount .') ORDER BY `id` DESC';
				}
					
			}else
				$sqlCount = ' WHERE id = 0';

			$sqlCount = 'SELECT * FROM `'.DB_PRE.'file` '. $sqlCount;
			if( '' == $keys ) $sqlCount = 'SELECT * FROM `'.DB_PRE.'file` WHERE 1 = 2';
 
		}else{// 按名称
			
			$keyWords = $keys ;
			$keyWords = str_replace(array('　','+',',','，'),' ' , $keyWords);
			$keyWords = explode(' ',$keyWords);
			$keyWords = array_unique($keyWords);
			$where = 'where (';		
			foreach($keyWords as $keyWord){
				if(!$keyWord) continue ;
				$where .= 'or `name` like "%'. $keyWord .'%"' ;// or `description` like "%'. $keyWord .'%"  ';
			}
			$where = str_replace('where (or','where (',$where);		
			if( $where == 'where (')
				$where = '';
			else
				$where .= ' )';	
			
			if( $user -> powder() >= 9 ){
				$where = $where == '' ? ' WHERE ' : $where .' AND ';
				$where .= ' ( 1=1) ';
				$sqlCount = 'SELECT * FROM `'.DB_PRE.'file` '.$where.' ORDER BY `hits` DESC' ; //group by `name` 
			}elseif(  $user -> id() != 0  ){
				$where = $where == '' ? ' WHERE ' : $where .' AND ';
				$where .= ' (( `status` = "1"  ) OR ( `status` != "1" AND `user_id` = '. $user -> id() .' )) ';
				$sqlCount = 'SELECT * FROM `'.DB_PRE.'file` '.$where.' ORDER BY `hits` DESC' ;//group by `name` 
			}else{
				$where = $where == '' ? ' WHERE ' :  $where .' AND ';
				$where .= ' ( `status` = "1" )';
				$sqlCount = 'SELECT * FROM `'.DB_PRE.'file` '.$where.' ORDER BY `hits` DESC' ;//group by `name` 
			}
			if( '' == $keys ) $sqlCount = 'SELECT * FROM `'.DB_PRE.'file` WHERE 1 = 2';
		}
		
		//echo '['.$where .']' ;
		
		$sql = $sqlCount .' limit '. (($page - 1) *  PAGE_SIZE ) .' , '. PAGE_SIZE;		
		$lists	= $dbs -> select($sqlCount);
		$total	= count($lists);
//		echo $sql;
//		echo '$total='.$total ;
		$lists = $dbs -> select($sql);
		$html = '';
		$html .='<table width="100%" border="0" cellspacing="1" cellpadding="3">'
				  .'<tr class="listTitle">'
					.'<th class="works">作品</th>'
					//.'<th class="name">名称</th>'
					//.'<th class="time">时间</th>'
					.'<th class="score">得分</th>'
					//.'<th class="hits">点击</th>'
					//.'<th class="down">下载</th>'
				//	.'<th class="comm">评论</th>'
					.'<th class="operate">'.lang_operate.'</th>'
				  .'</tr>';
		foreach($lists as $list){
			
			//$list['name'] = subString(htmlShow($list['name'],<>),60);
			foreach($keyWords as $keyWord){
				if(!$keyWord) continue ;
				$list['name'] = str_replace( $keyWord,'<b class="high">'.$keyWord.'</b>',$list['name']) ;
			}
/*			$starHtml = '';
			for($i = 1; $i<= 5 ;$i++){
				$class = $i <= $list['star'] ? ' class="a" ' : '';
				$starHtml .= '<i '.$class.'></i>';
			}
			$id  = $encrypt -> code($list['id']);
			$userInfo = $user -> getUser($list['user_id']);*/
/*			$html .='<tr>'
				.'<td class="thumbs">'
					.'<div class="itemthumb">'
						.'<a title="'.htmlShow($list['name']).'" href="'.urlRewrite('option=media&id='.$id).'" target="_blank">'
							.'<img src="'.getMediaThumb($list['id']).'" alt="'.htmlShow($list['name']).'" />'
						.'</a>'
					.'</div>
					<div class="itemdetail">
						<h3 class="itemtitle"><a title="'.htmlShow($list['name']).'" href="'.urlRewrite('option=media&id='.$id).'" target="_blank">'.$names.'</a></h3>
						<p class="itemuploader">上传者：<a target="_blank" href="'.urlRewrite('option=home&id='.$list['user_id']).'">'. htmlShow($userInfo['nickName']) .'</a></p>
						<p class="itemtag">标签：'. getTag($list['id'],$target) .'</p>
						<p class="itemdate">上传时间：'. date('Y-m-d',strtotime($list['time'])) .'</p>
						<p><span>点击：'. $list['hits'] .'</span><span>下载：'. $list['downTimes'] .'</span><span>评价：'. $list['comments'] .'</span></p>
					</div>
				</td>'
				//.'<td>'. date('Y-m-d',strtotime($list['time'])) .'</td>'
				.'<td><span class="star">'.$starHtml.'</span></td>'
				//.'<td>'. $list['hits'] .'</td>'
				//.'<td>'. $list['downTimes'] .'</td>'
				//.'<td>'. $list['comments'] .'</td>'
				.'<td></td>'
				//<span class="action view"><a class="view" target="_blank" href="./?option=media&id='.$id.'">查看</a></span><span class="action mod"><a class="edit" target="_blank" href="./?option=upload&id='.$id.'">修改</a></span><span class="action del"><a class="del" href="./?option=userDo&type=action&do=delFile&id='.$id.'">删除</a></span>
			  .'</tr>'
			  .'<tr><td colspan="10" class="compart"><hr /></td></tr>';*/
			$html .= fileListHtml($list);
			 
		}
		if( count( $lists ) == 0 ){
			$html .= '<tr><td colspan="10" class="compart"><p class="noExist">没有结果,请缩短“关键词”，再次搜索</p></td></tr>';
		}
		$html .= '</table>';
		$html .= getPages($page,$total,getPageFileUrl('key='. $keys .'',false),PAGE_SIZE, PAGE_LEN,false);
		echo $html;
	}
}
?>